package com.cogent.business.helper;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dynamicform.cache.EnumCacheManager;

public class EnumHelper {
	public static String getEnumValue(String code, String value) {
		Map enumMap = (Map)EnumCacheManager.get(code + "!MAP");
		return MapUtil.getStringValue(enumMap, value);
	}
	
	public static String getEnumRadio(String name,String code, String className, String value) {
		StringBuffer str = new StringBuffer();
		List enmuList = (List)EnumCacheManager.get(code + "!LIST");
		Iterator it = enmuList.iterator();
		while(it.hasNext()) {
			HashMap map = (HashMap)it.next();
			String val = MapUtil.getStringValue(map, "value");
			if(StringUtil.isEmpty(val))
				continue;
			str.append("<input  type='radio' name =\"").append(name).append("\"").append(" id =\"").append(name).append(val).append("\""); 
			str.append("  value=\"").append(val).append("\"");
 			if(val.equals(value))
 				str.append("checked=\"checked\"");
			str.append("/>").append("<label for=\"").append(name).append(val).append("\">").append(MapUtil.getStringValue(map, "enumitemname")).append("</label>");
		} 
		return str.toString();	
	}
	
	
	public static String getEnumSelect(String name, String code, String className, String value) {
		StringBuffer str = new StringBuffer();
		str.append("<select name=\"").append(name).append("\", id=\"").append(name).append("\" class=\"").append(className).append("\">");
		List enmuList = (List)EnumCacheManager.get(code + "!LIST");
		Iterator it = enmuList.iterator();
		while(it.hasNext()) {
			HashMap map = (HashMap)it.next();
			String val = MapUtil.getStringValue(map, "value");
			if(StringUtil.isEmpty(val))
				continue;
			str.append("<option value=\"").append(val).append("\"");
			if(val.equals(value))
				str.append(" selected=\"selected\"");
			str.append(">").append(MapUtil.getStringValue(map, "enumitemname")).append("</option>");
		}
		str.append("</select>");
		
		return str.toString();	
	}
	
	public static String getEnumNoSelect(String name, String code, String className, String value) {
		StringBuffer str = new StringBuffer();
		str.append("<select name=\"").append(name).append("\", id=\"").append(name).append("\" class=\"").append(className).append("\">");
		str.append("<option value=\"\">--请选择</option>");
		List enmuList = (List)EnumCacheManager.get(code + "!LIST");
		Iterator it = enmuList.iterator();
		while(it.hasNext()) {
			HashMap map = (HashMap)it.next();
			String val = MapUtil.getStringValue(map, "value");
			if(StringUtil.isEmpty(val))
				continue;	
			str.append("<option value=\"").append(val).append("\"");
			if(val.equals(value))
				str.append(" selected=\"selected\"");
			str.append(">").append(MapUtil.getStringValue(map, "enumitemname")).append("</option>");
		}
		str.append("</select>");
		
		return str.toString();		
	}	
	 
	@SuppressWarnings("unchecked")
	public static String getEnumNoSelect(String name, String code, String className, String value,List omitvalues) {
		StringBuffer str = new StringBuffer();
		str.append("<select name=\"").append(name).append("\", id=\"").append(name).append("\" class=\"").append(className).append("\">");
		str.append("<option value=\"\">--请选择</option>");
		List enmuList = (List)EnumCacheManager.get(code + "!LIST");
		Iterator it = enmuList.iterator();
		while(it.hasNext()) {
			HashMap map = (HashMap)it.next();
			String val = MapUtil.getStringValue(map, "value");
			if(StringUtil.isEmpty(val))
				continue;
			Iterator ite=omitvalues.iterator();
			boolean isomit=false;
			while(ite.hasNext()){
				String omitvalue=(String)ite.next();
				if(omitvalue.equals(val))
                  isomit=true;
			}
			if(isomit)
				continue;
			str.append("<option value=\"").append(val).append("\"");
			if(val.equals(value))
				str.append(" selected=\"selected\"");
			str.append(">").append(MapUtil.getStringValue(map, "enumitemname")).append("</option>");
		}
		str.append("</select>");
		
		return str.toString();		
	}
	
	public static String getEnumNoSelect(String name, String code, String className, String value, String onchage,List omitvalues) {
		StringBuffer str = new StringBuffer();
		str.append("<select name=\"").append(name).append("\", id=\"").append(name).append("\" class=\"").append(className)
			.append("\" onchange=\"").append(onchage).append("\">");
		str.append("<option value=\"\">--请选择</option>");
		List enmuList = (List)EnumCacheManager.get(code + "!LIST");
		Iterator it = enmuList.iterator();
		while(it.hasNext()) {
			HashMap map = (HashMap)it.next();
			String val = MapUtil.getStringValue(map, "value");
			if(StringUtil.isEmpty(val))
				continue;	
			Iterator ite=omitvalues.iterator();
			boolean isomit=false;
			while(ite.hasNext()){
				String omitvalue=(String)ite.next();
				if(omitvalue.equals(val))
                  isomit=true;
			}
			if(isomit)
				continue;
			str.append("<option value=\"").append(val).append("\"");
			if(val.equals(value))
				str.append(" selected=\"selected\"");
			str.append(">").append(MapUtil.getStringValue(map, "enumitemname")).append("</option>");
		}
		str.append("</select>");
		
		return str.toString();		
	}
	
	public static String getEnumNoSelect(String name, String code, String className, String value, String onchage) {
		StringBuffer str = new StringBuffer();
		str.append("<select name=\"").append(name).append("\", id=\"").append(name).append("\" class=\"").append(className)
			.append("\" onchange=\"").append(onchage).append("\">");
		str.append("<option value=\"\">--请选择</option>");
		List enmuList = (List)EnumCacheManager.get(code + "!LIST");
		Iterator it = enmuList.iterator();
		while(it.hasNext()) {
			HashMap map = (HashMap)it.next();
			String val = MapUtil.getStringValue(map, "value");
			if(StringUtil.isEmpty(val))
				continue;	
			str.append("<option value=\"").append(val).append("\"");
			if(val.equals(value))
				str.append(" selected=\"selected\"");
			str.append(">").append(MapUtil.getStringValue(map, "enumitemname")).append("</option>");
		}
		str.append("</select>");
		
		return str.toString();		
	}
	
	public static List getEnumList(String code) {
		List enmuList = (List)EnumCacheManager.get(code + "!LIST");
		
		return enmuList;
	}
}
